﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Sales;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Sales;
using ESOL.Controls;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainInvoicingApplication : BaseForm.frmBaseDialogyBusinessForm
    {
        #region vars
        public FinBillingReceivedApplicationEntity SelectedBillingApplication { get; set; }
        private List<SalesDeliveryItemEntity> mListDeliveryItem;
        private int mModifyType = 0;//0:=修改；1=开票；2=送票；3=送票回馈
        public int ModifyType
        {
            get { return mModifyType; }
            set { mModifyType = value; }
        }
        private List<SalesDeliveryOrderEntity> salesDeliveryOrderEntities;
        #endregion

        #region ctors
        public frmMaintainInvoicingApplication()
        {
            InitializeComponent();
            if (SelectedBillingApplication == null)
            {
                SelectedBillingApplication = new FinBillingReceivedApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
            }
        }
        public frmMaintainInvoicingApplication(List<SalesDeliveryItemEntity> list, MstCustomerEntity customer)
        {
            InitializeComponent();
            if (SelectedBillingApplication == null)
            {
                SelectedBillingApplication = new FinBillingReceivedApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
            }

            mListDeliveryItem = list;
            txtCustomer.Tag = customer;

            if (list != null && list.Count > 0)
            {
                string condition = " AND SDO.DELIVERY_ORDER_ID = '" + list[0].DeliveryOrderId + "'";
                DataTable dt = SalesDeliveryItemBs.GetByCondition(condition, " SDO.DELIVERY_ORDER_CODE");
                gvMain.DataSource = GetNewDataTable(dt);
                ResetItemAmount();
            }
        }
        #endregion

        #region functions
        private void SetFieldsByCustomer(MstCustomerEntity customer)
        {
            if (customer != null)
            {
                txtCustomer.Tag = customer;
                txtCustomer.Text = customer.CustomerName;
                UICommonFunctions.BindContactCombobox(cmbCustomerContact, customer.CustomerId, false);
                if (cmbCustomerContact.Items.Count > 0)
                {
                    cmbCustomerContact.SelectedIndex = 0;
                    MstCustomerContactEntity entity = (MstCustomerContactEntity)cmbCustomerContact.SelectedItem;
                    if (entity != null)
                    {
                        txtCustomerContactPhone.Text = entity.ContactMobilephone;
                    }
                }
                MstPersonEntity person = MstPersonCustomerBs.GetPersonByCustomer(customer.CustomerId);
                if (person != null)
                {
                    txtApplicationPerson.Tag = person;
                    txtApplicationPerson.Text = person.NameCn;
                }
            }
            else
            {
                txtCustomer.Tag = null;
                txtCustomer.Text = "";
                UICommonFunctions.BindContactCombobox(cmbCustomerContact, "", false);
                txtCustomerContactPhone.Text = "";
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCurrency, "CS", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "BILLING", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbInvoiceSendingType, "IST", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbBillingType, "BILLTYPE", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPayType, "PAT", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItemWithCode(cmbOrderType, "CFT", false);
        }
        private void SetFields()
        {
            if (SelectedBillingApplication == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(SelectedBillingApplication.CustomerId) == false)
            {
                MstCustomerEntity customer = MstCustomerBs.GetMstCustomerByID(SelectedBillingApplication.CustomerId);
                SetFieldsByCustomer(customer);
            }
            dtpApplicationDate.EditValue = SelectedBillingApplication.ApplicationDate;
            if (ThisActionType == ActionType.New)
            {
                dtpApplicationDate.EditValue = "";
                if (CommonParameter.CurrentUser.PersonEntity != null)
                {
                    txtApplicationPerson.Tag = CommonParameter.CurrentUser.PersonEntity;
                    txtApplicationPerson.Text = CommonParameter.CurrentUser.PersonEntity.NameCn + " (" + CommonParameter.CurrentUser.PersonEntity.PersonCode + ")";
                }
            }
            else
            {
                if (string.IsNullOrEmpty(SelectedBillingApplication.ApplicationPerson) == false)
                {
                    MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectedBillingApplication.ApplicationPerson);
                    if (person != null)
                    {
                        txtApplicationPerson.Tag = person;
                        txtApplicationPerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                    }
                }
            }
            txtBillingAmount.Text = SelectedBillingApplication.BillingAmount.ToString("#0.00");
            txtAppAmount.Text = SelectedBillingApplication.AppAmount.ToString("#0.00");
            txtApprovedAmount.Text = SelectedBillingApplication.ApprovedAmount.ToString("#0.00");
            cmbStatus.SetSelectedValue(SelectedBillingApplication.Status);
            cmbCurrency.SetSelectedValue(SelectedBillingApplication.Currency);
            txtApprovedRemark.Text = SelectedBillingApplication.ApprovedRemark;

            dtpBillingDate.EditValue = SelectedBillingApplication.BillingDate;
            txtInvoiceNo.Text = SelectedBillingApplication.InvoiceNo;
            txtBillingCompany.Text = SelectedBillingApplication.BillingCompany;

            cmbInvoiceSendingType.SetSelectedValue(SelectedBillingApplication.InvoiceSendingType);
            txtPostNo.Text = SelectedBillingApplication.InvoiceSendingPostNo;
            txtInvoiceSendingPerson.Text = SelectedBillingApplication.InvoiceSendingPerson;
            txtInvoiceReceibedPerson.Text = SelectedBillingApplication.InvoiceReceivedPerson;
            txtFeedBackRemark.Text = SelectedBillingApplication.FeedbackRemark;
            cmbPayType.SetSelectedValue(SelectedBillingApplication.PayType);
            cmbBillingType.SetSelectedValue(SelectedBillingApplication.BillingType);
            cmbOrderType.SetSelectedValue(SelectedBillingApplication.OrderType);

            txtRemark.Text = SelectedBillingApplication.Remark;

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, SelectedBillingApplication.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, SelectedBillingApplication.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, SelectedBillingApplication.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, SelectedBillingApplication.LastModifiedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, SelectedBillingApplication.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, SelectedBillingApplication.ApprovedBy);
        }
        private void SetEntity()
        {
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                SelectedBillingApplication.CustomerId = ((MstCustomerEntity)txtCustomer.Tag).CustomerId;
            }
            if (dtpApplicationDate.EditValue != null)
            {
                SelectedBillingApplication.ApplicationDate = Convert.ToDateTime(dtpApplicationDate.EditValue);
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                SelectedBillingApplication.ApplicationPerson = ((MstPersonEntity)txtApplicationPerson.Tag).PersonId;
            }
            SelectedBillingApplication.ApprovedRemark = txtApprovedRemark.Text;

            SelectedBillingApplication.BillingAmount = CommonFunctions.ToDecimal(txtBillingAmount.Text);
            SelectedBillingApplication.AppAmount = CommonFunctions.ToDecimal(txtAppAmount.Text);
            SelectedBillingApplication.ApprovedAmount = CommonFunctions.ToDecimal(txtApprovedAmount.Text);
            if (cmbCurrency.SelectedValue != null && cmbCurrency.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.Currency = cmbCurrency.SelectedValue.ToString();
            }
            if (dtpBillingDate.EditValue != null)
            {
                SelectedBillingApplication.BillingDate = Convert.ToDateTime(dtpBillingDate.EditValue);
            }
            SelectedBillingApplication.InvoiceNo = txtInvoiceNo.Text;

            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                SelectedBillingApplication.Status = "BILLING01";
            }

            if (cmbInvoiceSendingType.SelectedValue != null && cmbInvoiceSendingType.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.InvoiceSendingType = cmbInvoiceSendingType.SelectedValue.ToString();
            }
            if (cmbBillingType.SelectedValue != null && cmbBillingType.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.BillingType = cmbBillingType.SelectedValue.ToString();
            }
            if (cmbOrderType.SelectedValue != null && cmbOrderType.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.OrderType = cmbOrderType.SelectedValue.ToString();
            }
            if (cmbPayType.SelectedValue != null && cmbPayType.SelectedValue.ToString().Trim() != "")
            {
                SelectedBillingApplication.PayType = cmbPayType.SelectedValue.ToString();
            }
            SelectedBillingApplication.InvoiceSendingPostNo = txtPostNo.Text;
            SelectedBillingApplication.InvoiceSendingPerson = txtInvoiceSendingPerson.Text;
            SelectedBillingApplication.BillingCompany = txtBillingCompany.Text;
            SelectedBillingApplication.FeedbackRemark = txtFeedBackRemark.Text;

            SelectedBillingApplication.Remark = txtRemark.Text;
            SelectedBillingApplication.InvoiceReceivedPerson = txtInvoiceReceibedPerson.Text;

            if (ThisActionType == ActionType.New)
            {
                SelectedBillingApplication.CreatedDate = DateTime.Now;
                SelectedBillingApplication.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                SelectedBillingApplication.LastModifiedDate = DateTime.Now;
                SelectedBillingApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }
        private bool CheckData()
        {
            if (ThisActionType == ActionType.New || mModifyType == 0)
            {
                if (string.IsNullOrEmpty(txtCustomer.Text))
                {
                    MessageBox.Show("客户不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtApplicationPerson.Text))
                {
                    MessageBox.Show("申请人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbCurrency.Text))
                {
                    MessageBox.Show("货币种类不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (dtpApplicationDate.EditValue == null)
                {
                    MessageBox.Show("申请日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtAppAmount.Text))
                {
                    MessageBox.Show("申请金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            else if (ThisActionType == ActionType.Approve)
            {
            }
            else if (mModifyType == 1)//开票
            {
                if (dtpBillingDate.EditValue == null)
                {
                    MessageBox.Show("发票日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(txtBillingAmount.Text))
                {
                    MessageBox.Show("发票金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbPayType.Text))
                {
                    MessageBox.Show("款项类别不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbOrderType.Text))
                {
                    MessageBox.Show("核算项目不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(cmbBillingType.Text))
                {
                    MessageBox.Show("发票类别不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            else if (mModifyType == 2)//送票
            {
            }
            else if (mModifyType == 3)//送票回馈
            {
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = FinBillingReceivedApplicationBs.SaveBillingApplication(true, SelectedBillingApplication, GetListMap());
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    if (mModifyType == 0)
                    {
                        SetEntity();
                        bool isSuccessful = FinBillingReceivedApplicationBs.SaveBillingApplication(false, SelectedBillingApplication, GetListMap());
                        if (!isSuccessful)
                        {
                            MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                    else if (mModifyType == 1)
                    {
                        SetEntity();
                        SelectedBillingApplication.Status = "BILLING04";//已开票
                        GetDeliveryOrdersByGrid();
                        bool isSuccessful = FinBillingReceivedApplicationBs.ApproveBillingApplication(SelectedBillingApplication, salesDeliveryOrderEntities);
                        if (!isSuccessful)
                        {
                            MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                    else if (mModifyType == 2)
                    {
                        SetEntity();
                        SelectedBillingApplication.Status = "BILLING05";//发票已送
                        bool isSuccessful = FinBillingReceivedApplicationBs.UpdateFinBillingReceivedApplication(SelectedBillingApplication);
                        if (!isSuccessful)
                        {
                            MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                    else if (mModifyType == 3)
                    {
                        SetEntity();
                        SelectedBillingApplication.Status = "BILLING06";//已回馈
                        bool isSuccessful = FinBillingReceivedApplicationBs.UpdateFinBillingReceivedApplication(SelectedBillingApplication);
                        if (!isSuccessful)
                        {
                            MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return false;
                        }
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    SetEntity();
                    SelectedBillingApplication.ApprovedDate = DateTime.Now;
                    SelectedBillingApplication.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = FinBillingReceivedApplicationBs.ApproveBillingApplication(SelectedBillingApplication, salesDeliveryOrderEntities);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void BindDataGrid()
        {
            DataTable dt = FinMapBillingDeliveryBs.GetDeliveryItemsByAppId(SelectedBillingApplication.ApplicationId);
            gvMain.DataSource = dt;
            ResetItemAmount();
            mListDeliveryItem = new List<SalesDeliveryItemEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                SalesDeliveryItemEntity entity = new SalesDeliveryItemEntity(dr);
                mListDeliveryItem.Add(entity);
            }
        }
        private DataTable GetNewDataTable(DataTable table)
        {
            DataTable dt = FinMapBillingDeliveryBs.GetDeliveryItemsByAppId("");
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow row = dt.NewRow();
                    foreach (DataColumn column in table.Columns)
                    {
                        if (dt.Columns.Contains(column.ColumnName))
                        {
                            row[column.ColumnName] = table.Rows[i][column.ColumnName];
                        }
                    }
                    dt.Rows.Add(row);
                }
            }
            return dt;
        }
        private List<FinMapBillingDeliveryEntity> GetListMap()
        {
            List<FinMapBillingDeliveryEntity> list = new List<FinMapBillingDeliveryEntity>();
            if (gvMain.DataSource == null)
            {
                return list;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            foreach (DataRow dr in dt.Rows)
            {
                FinMapBillingDeliveryEntity entity = new FinMapBillingDeliveryEntity(dr);
                list.Add(entity);
            }
            return list;
        }
        private void ResetFieldsEnable()
        {
            if (mModifyType == 0)
            {
                return;
            }
            foreach (Control con in pnlTop.Controls)
            {
                if (con is DevTextEdit)
                {
                    ((DevTextEdit)con).Properties.ReadOnly = true;
                }
                else if (con is EsolComboBox)
                {
                    ((EsolComboBox)con).Enabled = false;
                }
                else if (con is DevDataTimePicker)
                {
                    ((DevDataTimePicker)con).Enabled = false;
                }
            }
            if (mModifyType == 1)
            {
                txtInvoiceNo.Properties.ReadOnly = false;
            }
            else if (mModifyType == 2)
            {
                cmbInvoiceSendingType.Enabled = true;
                txtPostNo.Properties.ReadOnly = false;
                txtInvoiceSendingPerson.Properties.ReadOnly = false;
            }
            else if (mModifyType == 3)
            {
                txtInvoiceReceibedPerson.Properties.ReadOnly = false;
                txtFeedBackRemark.Properties.ReadOnly = false;
            }
        }
        private void ResetItemAmount()
        {
            decimal itemAmount = 0;
            DataTable dt = gvMain.DataSource as DataTable;
            if (dt == null || dt.Rows.Count == 0)
            {
                return;
            }
            foreach(DataRow dr in dt.Rows)
            {
                itemAmount += CommonFunctions.ToDecimal(dr["BILLING_AMOUNT"]);
            }
            txtItemAmount.Text = itemAmount.ToString("#0.00");
            decimal diffAmount = CommonFunctions.ToDecimal(txtBillingAmount.Text) - itemAmount;
            txtDiffAmount.Text = diffAmount.ToString("#0.00");
        }
        private void SetPanelEnable(DevGroupControl gobx, bool enable)
        {
            if (gobx != null && gobx.Controls != null)
            {
                foreach (Control con in gobx.Controls)
                {
                    if (con.GetType() == typeof(DevTextEdit))
                    {
                        ((DevTextEdit)con).Properties.ReadOnly = !enable;
                    }
                    else if (con.GetType() == typeof(EsolComboBox))
                    {
                        con.Enabled = enable;
                    }
                    else if (con.GetType() == typeof(DevDataTimePicker))
                    {
                        con.Enabled = enable;
                    }
                }
            }
        }
        private void SetFieldsEnable()
        {
            if (ThisActionType == ActionType.New)
            {
                //SetPanelEnable(gboxApplication, false);
                SetPanelEnable(gboxApproved, false);
                SetPanelEnable(gboxBilling, false);
                SetPanelEnable(gboxFeedback, false);
            }
            else if (ThisActionType == ActionType.Modify)
            {
                if (mModifyType == 0)
                {
                    //SetPanelEnable(gboxApplication, false);
                    SetPanelEnable(gboxApproved, false);
                    SetPanelEnable(gboxBilling, false);
                    SetPanelEnable(gboxFeedback, false);
                }
                else if (mModifyType == 1)
                {
                    SetPanelEnable(gboxApplication, false);
                    SetPanelEnable(gboxApproved, false);
                    //SetPanelEnable(gboxBilling, false);
                    SetPanelEnable(gboxFeedback, false);
                }
                else if (mModifyType == 2 || mModifyType == 3)
                {
                    SetPanelEnable(gboxApplication, false);
                    SetPanelEnable(gboxApproved, false);
                    SetPanelEnable(gboxBilling, false);
                    //SetPanelEnable(gboxFeedback, false);
                }
            }
            else if (ThisActionType == ActionType.Approve)
            {
                SetPanelEnable(gboxApplication, false);
                //SetPanelEnable(gboxApproved, false);
                SetPanelEnable(gboxBilling, false);
                SetPanelEnable(gboxFeedback, false);
            }
        }
        //获取发货单明细
        private void GetDeliveryOrdersByGrid()
        {
            if (salesDeliveryOrderEntities == null)
            {
                salesDeliveryOrderEntities = new List<SalesDeliveryOrderEntity>();
            }
            DataTable dtDeliveryOrder = gvMain.DataSource as DataTable;
            foreach (DataRow dr in dtDeliveryOrder.Rows)
            {
                SalesDeliveryOrderEntity m = SalesDeliveryOrderBs.GetDeliveryOrderByCode(dr["DELIVERY_ORDER_CODE"].ToString());
                if (m != null)
                {
                    salesDeliveryOrderEntities.Add(m);
                }
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetFields();
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                SetFieldsByCustomer((MstCustomerEntity)txtCustomer.Tag);
            }
            //ResetFieldsEnable();
            SetFieldsEnable();
            BindDataGrid();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_SaveNew()
        {
            if (Save())
            {
                ThisActionType = ActionType.New;
                SelectedBillingApplication = new FinBillingReceivedApplicationEntity();
                SelectedBillingApplication.ApplicationId = Guid.NewGuid().ToString();
                SetFields();
            }
        }
        protected override void Click_Approve()
        {
            if (SelectedBillingApplication == null)
            {
                return;
            }
            SelectedBillingApplication.Status = "BILLING02";
            if (string.IsNullOrEmpty(txtApprovedAmount.Text))
            {
                MessageBox.Show("审核金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Reject()
        {
            if (SelectedBillingApplication == null)
            {
                return;
            }
            SelectedBillingApplication.Status = "BILLING03";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            frm.ShowAllCustomer = true;
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                SetFieldsByCustomer(frm.SelectedCustomer);
            }
            else
            {
                SetFieldsByCustomer(null);
            }
        }
        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }

        private void txtBillingAmount_Leave(object sender, EventArgs e)
        {
            ResetItemAmount();
        }

        private void tsmNewLine_Click(object sender, EventArgs e)
        {
            DataTable dt = gvMain.DataSource as DataTable;
            DataRow drNew = dt.NewRow();
            dt.Rows.Add(drNew);
        }

        private void tsmDelLine_Click(object sender, EventArgs e)
        {
            if (drSelected == null)
            {
                return;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            dt.Rows.Remove(drSelected);
            ResetItemAmount();
        }

        private DataRow drSelected;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.FocusedRowHandle);
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (ThisActionType == ActionType.Approve || ThisActionType == ActionType.View || mModifyType != 0)
            {
                return;
            }
            frmMaintainDeliveryItemsQuery frm = new Sales.frmMaintainDeliveryItemsQuery(true);
            frm.BillingStatus = "未开票";
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDataTable != null)
            {
                mListDeliveryItem = new List<SalesDeliveryItemEntity>();
                DataTable dt = GetNewDataTable(frm.SelectedDataTable);
                gvMain.DataSource = dt;
                foreach (DataRow dr in dt.Rows)
                {
                    SalesDeliveryItemEntity entity = new SalesDeliveryItemEntity(dr);
                    mListDeliveryItem.Add(entity);
                }
            }
            else
            {
                mListDeliveryItem = null;
            }
            ResetItemAmount();
        }
        #endregion
    }
}
